package com.his.emr.print.utils;

import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;

public class MyDriver {
	private static Properties mainconf = null;
	
	private static WebDriver staticDriver = null; 
	

	private static Properties prop(){
		if(null == mainconf){
			/**
			 * config.properties 写成  conf.properties
			 * 晕《,这样的问题遇到好多次了,CTM!!!
			 */
			String resPath = "config.properties"; 
			mainconf = My.prop(resPath);
		}
		return mainconf;
	}
	
	/**
	 * 
	 * Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies
	 * 
	 * @return
	 */
	public static WebDriver getPhantomJSDriver(){
		String _env = System.getProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY); 
		if(null == _env || "".equals(_env)){
			System.setProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
					prop().getProperty("phantomjsBinary") );
		}
		WebDriver driver = new PhantomJSDriver();
		return driver;
	}
	
	/**
	 * Add : 2018-2-52
	 * 获取WebDriver
	 * 
	 * @return
	 */
	public static WebDriver getStaticDriver(){
		if(null == staticDriver){
			System.setProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
					prop().getProperty("phantomjsBinary") );
			staticDriver = new PhantomJSDriver();
			/**
			 * Add 2018年2月15日<br>
			 * 首先打开一个没有内容的html,占用一个窗口,其他的窗口就可以调用 WebDriver.close()关闭掉!
			 * String url = My.fileUrl(My.rootpath("com/his/emr/print/html/myDriverFirstPage.html"));
			 * staticDriver.get( url );
			 * 测试发现,没必要这样处理!
			 */
		}
		return staticDriver;
	}
	
	/**
	 * 退出WebDriver
	 * 
	 */
	public static void closeStaticDriver(){
		if(null != staticDriver){
			staticDriver.quit();
			staticDriver = null;
		}
	}
	
	/**
	 * {@link org.openqa.selenium.WebDriver#close()} : Close the current window, quitting the browser if it's the last window currently open. <br>
	 * Add 2018年2月15日 : 避免开太多页签(之前没有意识到这个问题)<br>
	 * 经测试发现,`close`会导致后续调用都失败,所以此方法不能被调用
	 * 
	 */
	public static void closeCurrentWindow(){
		if(null != staticDriver){
			staticDriver.close();
		}
	}
	
	
}
